﻿
Imports std
Imports storoomc

Public Module deleterec
    Public Class arguments
        Public Const host As String = "host"
        Public Const port As String = "port"
        Public Const listfile As String = "l"
        Public Const yes As String = "y"
    End Class

    Public Const defaultListfile As String = "deleterec.lst"

    Public Sub main(ByVal iargs() As String)
        debugpause()
        Dim args As map(Of String, vector(Of String)) = Nothing
        argmgr.parse(iargs, args)
        Dim host As String = Nothing
        If Not argmgr.getfirstvalue(args, arguments.host, host) Then
            raiseError("do not have a host", errorHandle.errorType.user)
            Return
        End If
        Dim port As Int32 = npos
        If Not argmgr.getfirstvalue(args, arguments.port, port) Then
            raiseError("do not have a port", errorHandle.errorType.user)
            Return
        End If
        Dim listfile As String = Nothing
        If Not argmgr.getfirstvalue(args, arguments.listfile, listfile) Then
            raiseError("do not have a listfile, use default value as " + defaultListfile, errorHandle.errorType.user)
            listfile = defaultListfile
        End If
        storoom.initial(host, port)
        Dim yes As Boolean = False
        yes = argmgr.contains(args, arguments.yes)

        Dim s As String = Nothing
        If readfile(listfile, s) Then
            Dim v As vector(Of String) = Nothing
            strsepar(s, character.newline, v)
            Dim i As Int64
            For i = 0 To v.size() - 1
                kickLeftRightBlank(v(i))
                If Not isemptystring(v(i)) Then
                    Dim del As Boolean
                    If Not yes Then
                        Console.Write("delete record " + v(i) + "? ")
                        del = strsame(Console.ReadLine(), "y", strlen("y"), False)
                    Else
                        del = True
                    End If
                    If del Then
                        If isdebugmode() Then
                            raiseError("delete record " + v(i))
                        End If
                        wfileis.data.record.delete(v(i), storoom)
                    End If
                End If
            Next
        Else
            raiseError("cannot read file " + listfile, errorHandle.errorType.warning)
        End If
    End Sub
End Module
